package cn.bjxy.javaks.store_ms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bjxy.javaks.store_ms.entity.AdminEntity;
import cn.bjxy.javaks.store_ms.utils.JdbcUtil;

/**
 * 管理员账户管理数据访问层实现
 * @author long
 *
 */
public class AdminDaoImpl implements IAdminDao {

	private Connection con;
	private PreparedStatement pStat;
	private ResultSet rs;
	
	@Override
	public List<AdminEntity> findAll() {

		try {
			String sql = "SELECT * FROM admin_info";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			rs = pStat.executeQuery();
			
			ArrayList<AdminEntity> admins = new ArrayList<AdminEntity>();
			//遍历结果集
			while(rs.next()) {
				AdminEntity admin = new AdminEntity();   //封装查询到的管理员信息
				admin.setId(rs.getString("id"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setSex(rs.getBoolean("sex"));
				admin.setLevel(Integer.parseInt(rs.getString("level")));
				admin.setAdd_date(rs.getTimestamp("add_date"));
				admin.setModify_date(rs.getTimestamp("modify_date"));
				admins.add(admin);
			}
			return admins;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}


	@Override
	public AdminEntity findById(String id) {
		try {
			String sql = "SELECT * FROM admin_info WHERE id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, id);
			rs = pStat.executeQuery();
			
			AdminEntity admin = new AdminEntity();   //封装查询到的管理员信息
			//遍历结果集
			if(rs.next()) {
				admin.setId(rs.getString("id"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setSex(rs.getBoolean("sex"));
				admin.setLevel(Integer.parseInt(rs.getString("level")));
				admin.setAdd_date(rs.getTimestamp("add_date"));
				admin.setModify_date(rs.getTimestamp("modify_date"));
			}
			return admin;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public List<AdminEntity> findByName(String name) {
		try {
			String sql = "SELECT * FROM admin_info WHERE name=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, name);
			rs = pStat.executeQuery();
			
			ArrayList<AdminEntity> admins = new ArrayList<AdminEntity>();
			//遍历结果集
			while(rs.next()) {
				AdminEntity admin = new AdminEntity();   //封装查询到的管理员信息
				admin.setId(rs.getString("id"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setSex(rs.getBoolean("sex"));
				admin.setLevel(Integer.parseInt(rs.getString("level")));
				admin.setAdd_date(rs.getTimestamp("add_date"));
				admin.setModify_date(rs.getTimestamp("modify_date"));
				admins.add(admin);
			}
			return admins;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public AdminEntity findByNameAndPass(String name, String password) {
		try {
			String sql = "SELECT * FROM admin_info WHERE NAME=? AND PASSWORD=MD5(?)";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, name);
			pStat.setString(2, password);
			rs = pStat.executeQuery();
			
			AdminEntity admin = null;   //封装查询到的管理员信息
			//遍历结果集
			if(rs.next()) {
				admin = new AdminEntity();
				admin.setId(rs.getString("id"));
				admin.setName(rs.getString("name"));
				admin.setPassword(rs.getString("password"));
				admin.setSex(rs.getBoolean("sex"));
				admin.setLevel(Integer.parseInt(rs.getString("level")));
				admin.setAdd_date(rs.getTimestamp("add_date"));
				admin.setModify_date(rs.getTimestamp("modify_date"));
			}
			return admin;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}
	
	@Override
	public int delete(String id) {
		try {
			String sql = "delete from admin_info where id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,id);
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int add(AdminEntity admin) {
		try {
			String sql = "INSERT INTO admin_info VALUES(?,?,MD5(?),?,?,?,?);";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,admin.getId());
			pStat.setString(2, admin.getName());
			pStat.setString(3, admin.getPassword());
			pStat.setInt(4, (admin.isSex()?0:1));
			pStat.setInt(5, admin.getLevel());
			pStat.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
			pStat.setTimestamp(7, new Timestamp(new Date().getTime()));
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int modify(AdminEntity admin) {
		try {
			String sql = "UPDATE admin_info SET name=?,PASSWORD=MD5(?),sex=?,LEVEL=? WHERE id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1, admin.getName());
			pStat.setString(2, admin.getPassword());
			pStat.setInt(3, (admin.isSex()?1:0));
			pStat.setInt(4, admin.getLevel());
			pStat.setString(5,admin.getId());
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}
}
